﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Server.Base;
using ZhimeiSoft.CharmManager.Server.DAL.Interface;

namespace ZhimeiSoft.CharmManager.Server.DAL
{
    public class CustomerDAL : BaseDal, ICustomerDAL
    {
        /// <summary>
        /// 设置SQL语句中的参数
        /// </summary>
        /// <param name="cmd">DbCommand对象</param>
        /// <param name="customer">客户实体类</param>
        private void AddInSqlParameters(ref DbCommand cmd, DBEntityCustomInfo customerInfo)
        {
            if (customerInfo == null || cmd == null)
                return;

           //DB.AddInParameter(cmd, "CUSTOMID", System.Data.DbType.Int32, customerInfo.CUSTOMID);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CNAME, DbType.String, customerInfo.CNAME);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CNICKNAME, DbType.String, customerInfo.CNICKNAME);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CBIRTHDAY, DbType.DateTime, customerInfo.CBIRTHDAY);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CBIRTHDAYTYPE, System.Data.DbType.String, customerInfo.CBIRTHDAYTYPE);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CHEIGHT, DbType.String, customerInfo.CHEIGHT);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CWIGHT, DbType.String, customerInfo.CWIGHT);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CMOBILEPHONE, DbType.String, customerInfo.CMOBILEPHONE);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CFAMLIYPHONE, DbType.String, customerInfo.CFAMLIYPHONE);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CZIPCODE, DbType.String, customerInfo.CZIPCODE);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CADDRESS, DbType.String, customerInfo.CADDRESS);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CINDEXCODE, DbType.String, customerInfo.CINDEXCODE);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CMAIL, DbType.String, customerInfo.CMAIL);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.SALONID, DbType.Int32, customerInfo.SALONID);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CREGISTERDATE, DbType.DateTime, customerInfo.CREGISTERDATE);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CANCELDATE, DbType.DateTime, customerInfo.CANCELDATE);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CSTATE, DbType.String, customerInfo.CSTATE);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CLASTREGISTERDATE, DbType.DateTime, customerInfo.CLASTREGISTERDATE);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CLASTCALL, DbType.DateTime, customerInfo.CLASTCALL);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CMEMO, DbType.String, customerInfo.CMEMO);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.CONSTELLATORY, DbType.String, customerInfo.CONSTELLATORY);
           DB.AddInParameter(cmd, DBEntityCustomInfo.FieldName.BLODDTYPE, DbType.String, customerInfo.BLODDTYPE);
        }

        /// <summary>
        /// 新增客户信息
        /// </summary>
        /// <param name="customer">客户信息实体类</param>
        /// <returns>客户ID</returns>
        public void InsertCustomer(DBEntityCustomInfo customerInfo)
        {
            //customerInfo.CUSTOMID = this.GetMaxID("CUSTOMINFO", "CUSTOMID") + 1;
            string strSQL = CustomerDAL_Const.SQL_INSERT_CUSTOMINFO;
            DbCommand cmdSelect = DB.GetSqlStringCommand(strSQL);
            AddInSqlParameters(ref cmdSelect, customerInfo);
            DB.ExecuteNonQuery(cmdSelect);

        }

        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="customer">客户信息实体类</param>
        public void UpdateCustomer(DBEntityCustomInfo customerInfo)
        {
            string strSQL = CustomerDAL_Const.SQL_UPDATE_CUSTOMINFO;
            DbCommand cmdSelect = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmdSelect, DBEntityCustomInfo.FieldName.CUSTOMERID, DbType.Int32, customerInfo.CUSTOMERID);
            AddInSqlParameters(ref cmdSelect, customerInfo);
            DB.ExecuteNonQuery(cmdSelect);

        }

        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="customerID">客户ID</param>
        public void DeleteCustomer(int customerID)
        {
            string strSQL = CustomerDAL_Const.SQL_DELETE_CUSTOMINFO;
            DbCommand cmdSelect = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmdSelect, DBEntityCustomInfo.FieldName.CUSTOMERID, System.Data.DbType.Int32, customerID);
            DB.ExecuteNonQuery(cmdSelect);
        }
        /// <summary>
        /// 根据用户状态获取信息
        /// </summary>
        /// <param name="sateStr">状态字符串</param>
        /// <returns>返回数据</returns>
        public System.Data.DataSet QueryCustomState(string stateStr)
        {
            DbCommand queryCommd;
            if (stateStr == string.Empty)
            {
                queryCommd = DB.GetSqlStringCommand(CustomerDAL_Const.SQL_QUERY_CUSTOMINFO);
            }
            else
            {
                queryCommd = DB.GetSqlStringCommand(CustomerDAL_Const.SQL_QUERY_CUSTOMINFO_STATE);
                DB.AddInParameter(queryCommd, DBEntityCustomInfo.FieldName.CSTATE, System.Data.DbType.String, stateStr);
            }
            return DB.ExecuteDataSet(queryCommd);
        }
        /// <summary>
        /// 根据用户ID获取用户信息
        /// </summary>
        /// <param name="customerID">用户ID</param>
        /// <returns></returns>
        public DataSet QueryCustomID(int customerID)
        {
            string strSQL = CustomerDAL_Const.SQL_QUERY_CUSTOMINFO_ID;
            DbCommand cmdSelect = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmdSelect, DBEntityCustomInfo.FieldName.CUSTOMERID, System.Data.DbType.Int32, customerID);
            return DB.ExecuteDataSet(cmdSelect);
        }
        /// <summary>
        /// 根据会员姓名查询信息
        /// </summary>
        /// <param name="customName"></param>
        /// <returns></returns>
        public DataSet QueryCustomName(string customName)
        {
            DbCommand cmdSelect = DB.GetSqlStringCommand(CustomerDAL_Const.SQL_QUERY_CUSTOMNAME);
            DB.AddInParameter(cmdSelect, DBEntityCustomInfo.FieldName.CNAME, DbType.String, customName);
            return DB.ExecuteDataSet(cmdSelect);
        }

    }
}
